//
//  TabPage.m
//  EFExtend
//
//  Created by mac on 15/6/27.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "TabBoard.h"


@interface TabBoard ()
@property (nonatomic,strong) HMUITapbarView *tab;

@end

@implementation TabBoard{
    NSMutableArray *boards;
}
@synthesize tab;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    boards = nil;
    self.tab = nil;
    HM_SUPER_DEALLOC();
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    [self initializtion];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initializtion{
    
    boards = [[NSMutableArray alloc]init];
    [boards addObject:@{@"title":NSLocalizedStringFromTable(@"TI_Main_Home", @"Localizable", nil),@"subtitle":@"首页",@"map":URLFOR_controllerWithNav(@"UI")}];
    [boards addObject:@{@"title":NSLocalizedStringFromTable(@"TI_Main_Social", @"Localizable", nil),@"subtitle":@"书城",@"map":URLFOR_controllerWithNav(@"instroduce")}];
    
    [boards addObject:@{@"title":NSLocalizedStringFromTable(@"TI_Main_Publish", @"Localizable", nil),@"subtitle":@"我的",@"map":URLFOR_controllerWithNav(@"Publish")}];
    
    tab = [[HMUITapbarView alloc]init];
    tab.space = 5;
    tab.backgroundColor = [UIColor md_amber_100];
    tab.barStyle = UITapbarStyleFitSize;
    tab.eventReceiver = self;
    for (NSDictionary *board in boards) {
        [tab addItemWithTitle:[board valueForKey:@"title"] imageName:nil size:CGSizeZero];
    }
    [self.view addSubview:tab];
    
    [tab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        make.width.mas_equalTo(self.view.mas_width);
        make.height.mas_equalTo(49);
    }];
    [tab setSelectedIndex:0];
    
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {//customNavLeftBtn
            [self backAndRemoveWithAnimate:YES];
        }else if ([btn is:@"rightBtn"]){//customNavRightBtn
            
        }
    }
}


ON_TapBar(signal){
    if ([signal is:[HMUITapbarView TAPNOSELECTED]]) {
        return;
    }
    NSDictionary *board = [boards safeObjectAtIndex:self.tab.selectedIndex];
    if (board) {
        if (self.tab.selectedIndex==2) {
            [self presentViewController:[UMFeedback feedbackModalViewController] animated:YES completion:^{
                
            }];

            return;
        }
        NSString *map = [board valueForKey:@"map"];
        NSString *title = [board valueForKey:@"title"];
        HMUIBoard *target = nil;
        BOOL isNew = NO;
        target = [self putChild:map isNew:&isNew];
        if (isNew) {
            [target.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.view.mas_top).offset(0);
                make.left.mas_equalTo(self.view.mas_left).offset(0);
                make.bottom.mas_equalTo(self.tab.mas_top).offset(0);
                make.right.mas_equalTo(self.view.mas_right).offset(0);
            }];
            target.title = title;
        }
    }
}


@end
